package com.mjh.zhujianju.controller.party;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import com.google.common.collect.Maps;
import com.livew.framework.utils.XTCurrentUser;
import com.mjh.zhujianju.service.party.BuildingExamResultService;
import com.mjh.zhujianju.vo.party.BuildingExamResultVO;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.exception.XTBusinessException;
import com.mjh.zhujianju.model.party.BuildingExamEO;
import com.mjh.zhujianju.service.party.BuildingExamService;
import com.mjh.zhujianju.service.party.BuildingExamTopicService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import com.mjh.zhujianju.vo.party.BuildingExamTopicVO;
import com.mjh.zhujianju.vo.party.BuildingExamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liuyang
 * @Description: 考试
 * @date 2018/8/18 15:53
 */
@RestController
@RequestMapping("/exam")
@Api("在线考试")
public class ExamController {

    @Autowired
    private BuildingExamTopicService buildingExamTopicService;

    @Autowired
    private BuildingExamService buildingExamService;

    @Autowired
    BuildingExamResultService buildingExamResultService;

    @Resource
    private SysUserService userService;

    @ApiOperation(value="2 党建->首页->在线考试->详情->参加考试", notes="用户开始参加考试,取出所有题目")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "考试课题id", required = true, dataType = "Long", paramType = "path")
    })
    public ResponseEntity<JsonResult> getTopicList(@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null !=userId){
                //判断是否在当前考试时间内
                BuildingExamEO buildingExamEO = buildingExamService.getByPrimaryKey(id);
                Date data = new Date();
                int ca1 = buildingExamEO.getStartTime().compareTo(data);
                int ca2 = buildingExamEO.getEndTime().compareTo(data);
                if (ca1 == -1 && ca2 == 1){
                    BuildingExamResultVO vo = buildingExamResultService.queryBuildingExamResultList(id,userId);
                    if (vo != null){
                        result.setResult("你已参加过该考试");
                        result.setStatus(ApiConstant.MSG_ERROR);
                    }else{
                        Map<String,Object> map = Maps.newHashMap();
                        List<BuildingExamTopicVO> list  = buildingExamTopicService.queryBuildingExamTopicForWXList(id);
                        map.put("list",list);
                        BuildingExamEO buildingExam  = buildingExamService.getByPrimaryKey(id);
                        if(buildingExam.getEndTime() != null && buildingExam.getStartTime() != null) {
                            long diff = (buildingExam.getEndTime().getTime()- data.getTime());
                            diff = diff / (1000 * 60);
                            map.put("timeInterval",diff);
                        }
                        map.put("dateNew",String.valueOf(System.currentTimeMillis()));
                        result.setResult(map);
                        result.setStatus(ApiConstant.MSG_SUCCESS);
                    }
                }else {
                    result.setResult("不在当前考试时间范围内");
                    result.setStatus(ApiConstant.MSG_ERROR);
                }
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }


    @ApiOperation(value="2 党建->首页->在线考试->详情->参加考试->试题提交", notes="用户开始参加考试,取出所有题目")
    @RequestMapping(value = "/examSubmit", method = RequestMethod.POST)
    public ResponseEntity<JsonResult> getExamSubmit(@RequestBody BuildingExamVO buildingExamVO){
        JsonResult result = new JsonResult();
        try {
            boolean b1 = StringUtils.isEmpty(buildingExamVO.getTestTime());
            boolean b3 = CollectionUtils.isEmpty(buildingExamVO.getBuildingExamTopicVOS());
            if (b1 || b3){
                result.setResult("参数不合法");
                result.setStatus(ApiConstant.MSG_NO_PARTY);
                return ResponseEntity.ok(result);
            }

            Long userId = XTCurrentUser.getCurrentUserId();
            if (null !=userId){
                buildingExamVO.setUserId(userId);
                Map<String,Object> map = buildingExamService.addExamUser(buildingExamVO);
                result.setResult(map);
                result.setStatus(ApiConstant.MSG_SUCCESS);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="党建->在线考试->查看考试结果", notes="根据用户信息和课题id查询当前用户的考试结果")
    @RequestMapping(value = "result/{examId}/{resultId}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "examId", value = "考试课题id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getExamResult(@PathVariable(value = "examId") Long examId,@PathVariable(value = "resultId") Long resultId){
        JsonResult result = new JsonResult();
        try {
            BuildingExamVO vo =  buildingExamService.findPersonalBuildingExamInfo(examId,resultId);
            result.setResult(vo);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }
}
